//
//  LBValidateError.m
//  QuickDialog
//
//  Created by xu on 14-9-23.
//
//

#import "LBValidateErrors.h"

@implementation LBValidateError

-(instancetype)initWithSource:(NSString*)source errorMessage:(NSString*)errorMessage{
    self = [super init];
    if(self){
        self.source = source;
        self.errorMessage = errorMessage;
    }
    return self;
}

-(instancetype)initWithErrorMessage:(NSString*)errorMessage{
    self = [super init];
    if(self){
        self.errorMessage = errorMessage;
    }
    return self;
}

@end


@implementation LBValidateErrors

-(instancetype)initWithErrorMessage:(NSString*)errorMessage{
    LBValidateError * error = [[LBValidateError alloc] initWithErrorMessage:errorMessage];
    return [self initWithError:error];
}

-(instancetype)initWithSource:(NSString*)source errorMessage:(NSString*)errorMessage{
    LBValidateError * error = [[LBValidateError alloc] initWithSource:source errorMessage:errorMessage];
    return [self initWithError:error];
}

-(instancetype)initWithError:(LBValidateError*)error{
    self = [super init];
    if(self){
        if(error)
            self.errors = [[NSMutableArray alloc] initWithObjects:error, nil];
    }
    return self;
}

-(instancetype)initWithErrors:(LBValidateErrors *)errors{
    self = [super init];
    if(self){
        if(errors && errors.hasErrors)
            self.errors = [[NSMutableArray alloc] initWithArray:errors.errors];
    }
    return self;
}

-(NSString *)errorDescription{
    if(self.errors && self.errors.count > 0){
        NSMutableString* errDesc = [[NSMutableString alloc] init];
        for(LBValidateError* err in self.errors){
            if(errDesc.length > 0)
                [errDesc appendString:@"\n"];
            [errDesc appendString:err.errorMessage];
        }
        return errDesc;
    }
    return nil;
}

-(void)addError:(LBValidateError*)error{
    if(error){
        if(!self.errors){
            self.errors = [[NSMutableArray alloc] init];
        }
        [self.errors addObject:error];
    }
}

-(void)addErrors:(LBValidateErrors *)errors{
    if(errors && errors.hasErrors){
        if(!self.errors){
            self.errors = [[NSMutableArray alloc] init];
        }
        [self.errors addObjectsFromArray:errors.errors];
    }
}

-(BOOL)hasErrors{
    if(self.errors && self.errors.count > 0)
        return YES;
    return NO;
}


@end
